package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzMonthlyReceiptOfGoods;
import com.gpframework.module.wages.domain.req.GzMonthlyReceiptOfGoodsAdd;
import com.gpframework.module.wages.domain.req.GzMonthlyReceiptOfGoodsReq;
import com.gpframework.module.wages.domain.req.GzMonthlyReceiptOfGoodsUpdate;
import com.gpframework.module.wages.domain.vo.GzMonthlyReceiptOfGoodsVo;
import com.gpframework.module.wages.service.IGzMonthlyReceiptOfGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzMonthlyReceiptOfGoodsController
 * @Author gpframework
 * @Date 2024-01-19
 * @Version 1.0.0
 * @Description 收货月度Controller
 */
@Validated
@Api(tags = "收货月度")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/monthlyreceiptofgoods" )
public class GzMonthlyReceiptOfGoodsController extends BaseController {

    private final IGzMonthlyReceiptOfGoodsService iGzMonthlyReceiptOfGoodsService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-19
  * @Param gzMonthlyReceiptOfGoods
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:monthlyreceiptofgoods:list')")
    @GetMapping("/list")
    public TableDataInfo<GzMonthlyReceiptOfGoodsVo> list(GzMonthlyReceiptOfGoodsReq gzMonthlyReceiptOfGoodsReq)
    {
        startPage();
        QueryWrapper<GzMonthlyReceiptOfGoods> lqw = new QueryWrapper<GzMonthlyReceiptOfGoods>();
        gzMonthlyReceiptOfGoodsReq.generatorQuery(lqw,true);
        List<GzMonthlyReceiptOfGoodsVo> list = iGzMonthlyReceiptOfGoodsService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyReceiptOfGoods
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:monthlyreceiptofgoods:add')" )
    @Log(title = "添加收货月度" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzMonthlyReceiptOfGoodsAdd gzMonthlyReceiptOfGoodsAdd) {

        iGzMonthlyReceiptOfGoodsService.remove(new QueryWrapper<GzMonthlyReceiptOfGoods>().eq("month", gzMonthlyReceiptOfGoodsAdd.getMonth()));

        GzMonthlyReceiptOfGoods gzMonthlyReceiptOfGoods = Convert.convert(new TypeReference<GzMonthlyReceiptOfGoods>() {}, gzMonthlyReceiptOfGoodsAdd);
        //小条金额=小条*小条单价
        gzMonthlyReceiptOfGoods.setSmallBarAmount(gzMonthlyReceiptOfGoodsAdd.getSmallBarUnitPrice().multiply(new BigDecimal(gzMonthlyReceiptOfGoodsAdd.getSmallBar() )));
        //大条金额=大条* 大条单价
        gzMonthlyReceiptOfGoods.setLargeBarAmount(gzMonthlyReceiptOfGoodsAdd.getUnitPriceOfLargeBars().multiply(new BigDecimal(gzMonthlyReceiptOfGoodsAdd.getBigBar()))) ;

        //退货再加金额=退货再加*退货再加单价
        gzMonthlyReceiptOfGoods.setReturnPlusAmount(gzMonthlyReceiptOfGoodsAdd.getReturnPlusUnitPrice().multiply(new BigDecimal(gzMonthlyReceiptOfGoodsAdd.getReturnAndAdd())));

        //收货实发 =小条金额+大条金额+退货再加金额+休假补贴+扣减补贴
        gzMonthlyReceiptOfGoods.setReceivedAndActuallyShipped(
                gzMonthlyReceiptOfGoods.getSmallBarAmount().add(gzMonthlyReceiptOfGoods.getLargeBarAmount()).add(gzMonthlyReceiptOfGoods.getReturnPlusAmount()
                                .add(gzMonthlyReceiptOfGoodsAdd.getVacationAllowance())
                                .add(gzMonthlyReceiptOfGoodsAdd.getDeductionOfSubsidies()))
        );
        gzMonthlyReceiptOfGoods.setCaptainAdditionalSubsidy(gzMonthlyReceiptOfGoods.getReceivedAndActuallyShipped());
        //  补帖实发合计=队长额外补贴 *额外补贴单价
        gzMonthlyReceiptOfGoods.setSupplementaryPost(gzMonthlyReceiptOfGoods.getCaptainAdditionalSubsidy
                ().multiply(gzMonthlyReceiptOfGoodsAdd.getAdditionalSubsidyUnitPrice()));

        //实发合计= 收货实发+补帖实发合计
        gzMonthlyReceiptOfGoods.setTotalPaidIn(
                gzMonthlyReceiptOfGoods.getReceivedAndActuallyShipped()
                        .add(gzMonthlyReceiptOfGoods.getSupplementaryPost())
        );
        gzMonthlyReceiptOfGoods.setDepartmentName("收货组");
        return toAjax(iGzMonthlyReceiptOfGoodsService.save(gzMonthlyReceiptOfGoods) ? 1 : 0);



    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyReceiptOfGoods
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('wages:monthlyreceiptofgoods:edit')" )
    @Log(title = "修改收货月度" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzMonthlyReceiptOfGoodsUpdate gzMonthlyReceiptOfGoodsUpdate) {
        GzMonthlyReceiptOfGoods gzMonthlyReceiptOfGoods = Convert.convert(new TypeReference<GzMonthlyReceiptOfGoods>() {}, gzMonthlyReceiptOfGoodsUpdate);

        //小条金额=小条*小条单价
        gzMonthlyReceiptOfGoods.setSmallBarAmount(gzMonthlyReceiptOfGoodsUpdate.getSmallBarUnitPrice().multiply(new BigDecimal(gzMonthlyReceiptOfGoodsUpdate.getSmallBar() )));
        //大条金额=大条* 大条单价
        gzMonthlyReceiptOfGoods.setLargeBarAmount(gzMonthlyReceiptOfGoodsUpdate.getUnitPriceOfLargeBars().multiply(new BigDecimal(gzMonthlyReceiptOfGoodsUpdate.getBigBar()))) ;

        //退货再加金额=退货再加*退货再加单价
        gzMonthlyReceiptOfGoods.setReturnPlusAmount(gzMonthlyReceiptOfGoodsUpdate.getReturnPlusUnitPrice().multiply(new BigDecimal(gzMonthlyReceiptOfGoodsUpdate.getReturnAndAdd())));

        //收货实发 =小条金额+大条金额+退货再加金额+休假补贴+扣减补贴
        gzMonthlyReceiptOfGoods.setReceivedAndActuallyShipped(
                gzMonthlyReceiptOfGoods.getSmallBarAmount().add(gzMonthlyReceiptOfGoods.getLargeBarAmount()).add(gzMonthlyReceiptOfGoods.getReturnPlusAmount()
                        .add(gzMonthlyReceiptOfGoodsUpdate.getVacationAllowance())
                        .add(gzMonthlyReceiptOfGoodsUpdate.getDeductionOfSubsidies()))
        );
        gzMonthlyReceiptOfGoods.setCaptainAdditionalSubsidy(gzMonthlyReceiptOfGoods.getReceivedAndActuallyShipped());
        //  补帖实发合计=队长额外补贴 *额外补贴单价
        gzMonthlyReceiptOfGoods.setSupplementaryPost(gzMonthlyReceiptOfGoods.getCaptainAdditionalSubsidy
                ().multiply(gzMonthlyReceiptOfGoodsUpdate.getAdditionalSubsidyUnitPrice()));

        //实发合计= 收货实发+补帖实发合计
        gzMonthlyReceiptOfGoods.setTotalPaidIn(
                gzMonthlyReceiptOfGoods.getReceivedAndActuallyShipped()
                        .add(gzMonthlyReceiptOfGoods.getSupplementaryPost())
        );
        gzMonthlyReceiptOfGoods.setDepartmentName("收货组");
        return toAjax(iGzMonthlyReceiptOfGoodsService.updateById(gzMonthlyReceiptOfGoods) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyReceiptOfGoods
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:monthlyreceiptofgoods:export')" )
    @Log(title = "导出收货月度" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzMonthlyReceiptOfGoodsReq gzMonthlyReceiptOfGoodsReq) {
        QueryWrapper<GzMonthlyReceiptOfGoods> lqw = new QueryWrapper<GzMonthlyReceiptOfGoods>();
        gzMonthlyReceiptOfGoodsReq.generatorQuery(lqw,true);
        List<GzMonthlyReceiptOfGoodsVo> list = iGzMonthlyReceiptOfGoodsService.listVo(lqw);
        ExcelUtil<GzMonthlyReceiptOfGoodsVo> util = new ExcelUtil<GzMonthlyReceiptOfGoodsVo>(GzMonthlyReceiptOfGoodsVo. class);
        return util.exportExcel(list, "monthlyreceiptofgoods" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyReceiptOfGoods
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:monthlyreceiptofgoods:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzMonthlyReceiptOfGoodsVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzMonthlyReceiptOfGoodsService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyReceiptOfGoods
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:monthlyreceiptofgoods:remove')" )
    @Log(title = "删除收货月度" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzMonthlyReceiptOfGoodsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
